package com.tjsinfo.tjsoa.addresslist.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserPageBo;
import com.tjsinfo.tjsoa.addresslist.dao.IAddressListGroupDAO;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListGroup;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class UpdateAddressListUserAction extends Action{
	private IAddressListGroupDAO listGroupDAO;
	private IAddressListUserBo userBo;
	private IAddressListUserPageBo listUserPageBo;
	private IAddressListUserBo listUserBo;

	public void setListUserBo(IAddressListUserBo listUserBo) {
		this.listUserBo = listUserBo;
	}
	public void setListGroupDAO(IAddressListGroupDAO listGroupDAO) {
		this.listGroupDAO = listGroupDAO;
	}
	public void setUserBo(IAddressListUserBo userBo) {
		this.userBo = userBo;
	}
	public void setListUserPageBo(IAddressListUserPageBo listUserPageBo) {
		this.listUserPageBo = listUserPageBo;
	}
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	private Date date = null;
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		HttpSession session = request.getSession();
		User users = (User) session.getAttribute("user");
		TSystemUser tjsuser = new TSystemUser();
		tjsuser.setUserId(users.getUser_id());
		AddressListUser listUser = new AddressListUser();
		
		String moduleType = request.getParameter("moduleType");
		String intpage = request.getParameter("intpage");
		String group_id = request.getParameter("group_id");
		String yuangroup_id = request.getParameter("yuangroup_id");
		AddressListGroup group = listGroupDAO.findById(group_id);
		
		String yuanuserSort = request.getParameter("yuanuserSort");
		String userSort = request.getParameter("userSort");
		
		String id = request.getParameter("id");
		listUser = listUserBo.getAddresssListUserById(id);
		String userName = request.getParameter("userName");
		String user_id = request.getParameter("user_id");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		
		String homeAddress = request.getParameter("homeAddress");
		String homePhone = request.getParameter("homePhone");
		String homePostal = request.getParameter("homePostal");
		
		String company = request.getParameter("company");
		String sector = request.getParameter("sector");
		String duty = request.getParameter("duty");
		String companyPhone = request.getParameter("companyPhone");
		String companyFax = request.getParameter("companyFax");
		String companyAddress = request.getParameter("companyAddress");
		String companyPostal = request.getParameter("companyPostal");
		
		String birthday = request.getParameter("birthday");
		String qq = request.getParameter("qq");
		String msn = request.getParameter("msn");
		String blog = request.getParameter("blog");
		String remarks = request.getParameter("remarks");
		
		listUser.setTjsUser(tjsuser);
		if (user_id != null && !"".equals(user_id)) {
			listUser.setUserId(Integer.valueOf(user_id));
		}
		listUser.setAddressListGroup(group);
		listUser.setAddressListGroup(group);
		listUser.setUserName(userName);
		
		listUser.setMobile(mobile);
		listUser.setEmail(email);
		listUser.setHomeAddress(homeAddress);
		listUser.setHomePhone(homePhone);
		listUser.setHomePostal(homePostal);
		listUser.setCompany(company);
		listUser.setSector(sector);
		listUser.setDuty(duty);
		listUser.setCompanyPhone(companyPhone);
		listUser.setCompanyFax(companyFax);
		listUser.setCompanyAddress(companyAddress);
		listUser.setCompanyPostal(companyPostal);
		if (birthday != null && !"".equals(birthday)) {
			try {
				date = sdf.parse(birthday);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listUser.setBirthday(date);
		}
		listUser.setQq(qq);
		listUser.setMsn(msn);
		listUser.setBlog(blog);
		listUser.setState(Byte.valueOf("1"));
		if (remarks != null && !"".equals(remarks)) {
			listUser.setRemarks(remarks.trim());
		}
		if (moduleType.equals("0")) {
			if (!yuangroup_id.equals(group_id)) {
				userSort = listUserBo.getMaxUserSort(group_id);
				String maxyuanSort = listUserBo.getMaxUserSort(yuangroup_id);
				String xianMaxSort = listUserBo.getMaxUserSort(group_id);
				int i = Integer.valueOf(maxyuanSort);
				if (!userSort.equals(xianMaxSort)) {
					listUser.setUserSort(Integer.valueOf("0"));
				}else {
					listUser.setUserSort(Integer.valueOf(userSort));
				}
				listUser.setId(id);
				userBo.updateAddressListUser(listUser);
				
				userBo.sortXianGtYuan(Integer.valueOf(yuanuserSort), i-1,yuangroup_id);
				
				if (!userSort.equals(xianMaxSort)) {
					userBo.sortYuanGtXian(Integer.valueOf(userSort), Integer.valueOf(xianMaxSort)-1,group_id);
					listUser.setUserSort(Integer.valueOf(userSort));
					userBo.updateAddressListUser(listUser);
				}
			}else {
				if (Integer.valueOf(yuanuserSort)>Integer.valueOf(userSort)) {
					userBo.delete(id);
					userBo.sortYuanGtXian(Integer.valueOf(yuanuserSort), Integer.valueOf(userSort),group_id);
					listUser.setUserSort(Integer.valueOf(userSort));
					userBo.saveAddressListUser(listUser);
				}else if (Integer.valueOf(yuanuserSort)<Integer.valueOf(userSort)) {
					userBo.delete(id);
					userBo.sortXianGtYuan(Integer.valueOf(yuanuserSort), Integer.valueOf(userSort),group_id);
					listUser.setUserSort(Integer.valueOf(userSort));
					userBo.saveAddressListUser(listUser);
				}else {
					listUser.setId(id);
					listUser.setUserSort(Integer.valueOf(userSort));
					userBo.updateAddressListUser(listUser);
				}
			}
		}else {
			listUser.setId(id);
			userBo.updateAddressListUser(listUser);
		}
		
		request.setAttribute("group_id", yuangroup_id);
		request.setAttribute("first", "updateorselect");
		request.setAttribute("user_id", listUser.getId().toString());
		request.setAttribute("user_page", intpage);
		request.setAttribute("moduleType", moduleType);
		return mapping.findForward("ok");
	}
}
